_base_ = [
    '../_base_/models/ssd300.py', '../_base_/datasets/coco_detection.py',
    '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py'
]
import os

# 修改训练轮次
train_cfg = dict(type='EpochBasedTrainLoop', max_epochs=150, val_interval=1)

# dataset settings
input_size = 300
# 修改类别数为2
model = dict(
    bbox_head=dict(
        type='SSDHead',
        num_classes=2,
    )
)

# 修改数据集相关配置（绝对路径）
dataset_type = 'SDB9KDataset'
data_root = '/root/mmdetection/dataset/SDB_9K_COCO/'
metainfo = {
    'classes': ('drone', 'bird'),
    'palette': [
        (253, 58, 52), (253, 159, 148),
    ]
}

train_pipeline = [
    dict(type='LoadImageFromFile', backend_args={{_base_.backend_args}}),
    dict(type='LoadAnnotations', with_bbox=True),
    dict(
        type='Expand',
        mean={{_base_.model.data_preprocessor.mean}},
        to_rgb={{_base_.model.data_preprocessor.bgr_to_rgb}},
        ratio_range=(1, 4)),
    dict(
        type='MinIoURandomCrop',
        min_ious=(0.1, 0.3, 0.5, 0.7, 0.9),
        min_crop_size=0.3),
    dict(type='Resize', scale=(input_size, input_size), keep_ratio=False),
    dict(type='RandomFlip', prob=0.5),
    dict(
        type='PhotoMetricDistortion',
        brightness_delta=32,
        contrast_range=(0.5, 1.5),
        saturation_range=(0.5, 1.5),
        hue_delta=18),
    dict(type='PackDetInputs')
]
test_pipeline = [
    dict(type='LoadImageFromFile', backend_args={{_base_.backend_args}}),
    dict(type='Resize', scale=(input_size, input_size), keep_ratio=False),
    dict(type='LoadAnnotations', with_bbox=True),
    dict(
        type='PackDetInputs',
        meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
                   'scale_factor'))
]
train_dataloader = dict(
    batch_size=4,
    num_workers=2,
    batch_sampler=None,
    dataset=dict(
        type=dataset_type,
        data_root=data_root,
        ann_file='train/annotations/SDB_9K_train.json',
        data_prefix=dict(img='train/images'),
        filter_cfg=dict(filter_empty_gt=True, min_size=32),
        pipeline=train_pipeline,
        backend_args={{_base_.backend_args}}))
val_dataloader = dict(
    batch_size=8,
    dataset=dict(
        type=dataset_type,
        ann_file='val/annotations/SDB_9K_val.json',
        backend_args=None,
        data_prefix=dict(img='val/images'),
        data_root=data_root,
        test_mode=True))
test_dataloader = dict(
    batch_size=8,
    dataset=dict(
        type=dataset_type,
        ann_file='test/annotations/SDB_9K_test.json',
        backend_args=None,
        data_prefix=dict(img='test/images'),
        data_root=data_root,
        test_mode=True,
    ))

val_evaluator = dict(
    ann_file=os.path.join(data_root, 'val/annotations/SDB_9K_val.json'),
    backend_args=None,
    format_only=False,
    metric='bbox',
    type='CocoMetric')
test_evaluator = dict(
    ann_file=os.path.join(data_root, 'test/annotations/SDB_9K_test.json'),
    backend_args=None,
    format_only=False,
    metric='bbox',
    type='CocoMetric')

# optimizer
optim_wrapper = dict(
    type='OptimWrapper',  # 官方lr=1e-3
    optimizer=dict(type='SGD', lr=2e-3, momentum=0.9, weight_decay=5e-4))

custom_hooks = [
    dict(type='NumClassCheckHook'),
    dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW')
]

# NOTE: `auto_scale_lr` is for automatically scaling LR,
# USER SHOULD NOT CHANGE ITS VALUES.
# base_batch_size = (8 GPUs) x (8 samples per GPU)
auto_scale_lr = dict(base_batch_size=64)
load_from = 'https://download.openmmlab.com/mmdetection/v2.0/ssd/ssd300_coco/ssd300_coco_20210803_015428-d231a06e.pth'
